package com.aysafety.iotdataplatformserver.configurer;

import com.aysafety.core.DBDriver.hbase.HBaseDriver;
import com.aysafety.iotdataplatformserver.listener.*;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.Charset;

@Component
public class IotDataPlatformServer {

    private Logger logger = LoggerFactory.getLogger(IotDataPlatformServer.class);

    public static Charset charse = Charset.forName("UTF-8");
    private int backLogParam;
    private boolean keepAlive;

    private final String tableTreeName = "ksTreeData";

    private final String[] tableTreeStructure = new String[] {"ksTreeData"};

    @Autowired
    IotDataPlatformServerInitializer iotDataPlatformServerInitializer;

    @Autowired
    HBaseConfigurer hBaseConfigurer;


    @Autowired
    ErrorListener errorListener;

    @Autowired
    MicroseismListener microseismListener;

    @Autowired
    NaturalGasListener naturalGasListener;

    @Autowired
    HydraulicSupportListener hydraulicSupportListener;

    @Autowired
    GeosoundListener geosoundListener;

    @Autowired
    AcousticEmissionIntensityListener acousticEmissionIntensityListener;

    @Autowired
    CurrentBreakerListener currentBreakerListener;

    @Autowired
    ElectromagneticIntensityListener electromagneticIntensityListener;

    @Autowired
    ElectromagneticPulseListener electromagneticPulseListener;

    @Autowired
    GasListener gasListener;

    @Autowired
    SonicPulseListener sonicPulseListener;

    @Autowired
    WindsListener windsListener;

    public void setParams(int backLogParam, boolean keepAlive) {
        this.backLogParam = backLogParam;
        this.keepAlive = keepAlive;
    }

    public void run(int port) throws Exception {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();

        try {
            logger.info("检查HBase存储表结构信息...");
            errorListener.createTable();
            /*microseismListener.createTable();
            microseismListener.createCalTable();
            microseismListener.createCalWarningTable();
            hydraulicSupportListener.createTable();
            hydraulicSupportListener.createCharacteristicTable();
            hydraulicSupportListener.createCalWarningTable();
            geosoundListener.createTable();
            geosoundListener.createCalTable();
            geosoundListener.createCalDailyTable();
            geosoundListener.createCalWarningTable();
            acousticEmissionIntensityListener.createTable();
            acousticEmissionIntensityListener.createCalTable();
            acousticEmissionIntensityListener.createCalWarningTable();
            currentBreakerListener.createTable();
            electromagneticIntensityListener.createTable();
            electromagneticIntensityListener.createCalTable();
            electromagneticIntensityListener.createCalWarningTable();
            electromagneticPulseListener.createTable();
            gasListener.createTable();
            gasListener.createCalTable();
            gasListener.createCalWarningTable();
            sonicPulseListener.createTable();
            windsListener.createTable();*/
            //HBaseDriver.createTable(this.tableTreeName, this.tableTreeStructure);
            ServerBootstrap sb = new ServerBootstrap();
            logger.info("netty服务启动设置参数:");
            logger.info("tcp队列SO_BACKLOG:" + String.valueOf(this.backLogParam));
            logger.info("tcp心跳检查状态:" + String.valueOf(this.keepAlive));
            logger.info("netty服务启动设置完成");
            sb.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(iotDataPlatformServerInitializer).option(ChannelOption.SO_BACKLOG, this.backLogParam)
                .childOption(ChannelOption.SO_KEEPALIVE, this.keepAlive);
            ChannelFuture cf = sb.bind(port).sync();
            logger.info("netty服务启动: [port:" + port + "]");
            cf.channel().closeFuture().sync();
        } catch (Exception e) {
            logger.info("netty服务错误", e);
            throw new Exception(e.getMessage());
        } finally {
            HBaseDriver.close();
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    }
}
